﻿// Copyright (c) 2012-2013 fancidev
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Text;

namespace Util.Media
{
    public interface IColorSampler
    {
        /// <summary>
        /// Samples the supplied bitmap. This method may be called multiple
        /// times to sample different bitmaps and the results are accumulated.
        /// </summary>
        /// <param name="pixels">Pixels in the bitmap, in ARGB format.</param>
        /// <param name="width">Width of the bitmap in pixels.</param>
        /// <param name="height">Height of the bitmap in pixels.</param>
        void Sample(int[] pixels, int width, int height);

        /// <summary>
        /// Gets the number of distinct colors in the sample.
        /// </summary>
        int ColorCount { get; }

        /// <summary>
        /// Gets the frequencies of each sampled color.
        /// </summary>
        /// <returns>
        /// For each KeyValuePair, Key is the ARGB representation of the
        /// color of the color, and Value is the frequency of that color
        /// in the sample.
        /// </returns>
        IEnumerable<KeyValuePair<int, int>> GetFrequencies();
    }

    public class UniformColorSampler : IColorSampler
    {
        private int dx;
        private int dy;
        private Dictionary<int, int> colorCount;

        public UniformColorSampler(int dx, int dy)
        {
            if (dx < 1 || dy < 1)
                throw new ArgumentOutOfRangeException("dx and dy must be greater than zero.");
            this.dx = dx;
            this.dy = dy;
            this.colorCount = new Dictionary<int, int>();
        }

        public UniformColorSampler() : this(1, 1) { }

        public void Sample(int[] pixels, int width, int height)
        {
            int numPixels = width * height;
            for (int y = 0; y < numPixels; y += dy * width)
            {
                for (int x = 0; x < width; x += dx)
                {
                    int color = pixels[y + x];
                    if (colorCount.ContainsKey(color))
                        colorCount[color]++;
                    else
                        colorCount[color] = 1;
                }
            }
        }

        public int ColorCount { get { return colorCount.Count; } }

        public IEnumerable<KeyValuePair<int, int>> GetFrequencies()
        {
            return colorCount;
        }
    }
}
